
	getRestaurantInfo();//餐厅信息
	findFoodCategoryList();//菜品分类列表对应菜品

	//添加食物个数逻辑
	function addFoodNum(addthis){

		var that = $(addthis).parents("li");//当前菜品模块的对象
		var thisClass = $(addthis).parents("li").attr("class");//当前菜品模块的类名
		
		var text = that.find(".subtract-other span").text();//当前菜品模块被选中的数量
		var totalPrice = 0;//总价格
		var foodPrice=0;
		var foodNum=0;
		var foodHtml = "";//添加已选择列表的html
		// console.log($("."+thisClass+"").text());
		if(that.find(".subtract-other").css("display")=="none"){
			$("."+thisClass+"").find(".subtract-other").css("display","block");
			var setHtml = "<div id='settlementDiv' style='height: 1.66rem;'></div>";
			$(".settlement").css("display","block");
			if($("#settlementDiv").height()==null){
				$(".settlement").before(setHtml);
			}
		}else{

			text++;
			$("."+thisClass+"").find(".subtract-other span").text(text);
			
		}
		// console.log($("."+thisClass+"").find(".foodNum").text());
		if($("."+thisClass+"").find(".foodNum").eq(0).text()=="1"){
			foodHtml = "<li class="+thisClass+" data-foodid="+$(that).attr("data-foodid")+"><div class='foodList_name'>"+$("."+thisClass+"").find(".detail-food-name").eq(0).text()+"</div>"
						+"<div class='foodList_price'>￥<span class='foodList_price_p'>"+$("."+thisClass+"").find(".foodMoney").eq(0).text()+"</span></div>"
						+"<div class='sub-add'>"
						+"<span class='subtract-other'><img class='subtractNum' src='./img/subtract_01.png' onclick='subtractFoodNum(this);'><span class='foodNum'>1</span></span>"
						+"<img class='addNum' src='./img/add_01.png' onclick='addFoodNum(this);'>"
						+"</div></li>";

			$(".foodList ul").append(foodHtml);
		}
		
		totalPrice= parseFloat($("#totalPrice").text())+parseFloat($("."+thisClass+"").find(".foodMoney").eq(0).text());
		
		$("#totalPrice").text(totalPrice);
		$(".settlement-car-num").text(parseFloat($(".settlement-car-num").text())+1);
	}
	//减少食物个数逻辑
	function subtractFoodNum(subthis){
		var that = $(subthis).parents("li");
		var thisClass = $(subthis).parents("li").attr("class");//当前菜品模块的类名
		var text = that.find(".subtract-other span").text();
		var totalPrice = 0;
		var foodPrice=0;
		var foodNum=0;
		
		if(text==1){
			$("."+thisClass+"").find(".subtract-other").css("display","none");
			
			// $("#totalPrice").text(0);
		}else{
			text--;
			$("."+thisClass+"").find(".subtract-other span").text(text);
			// $("#totalPrice").text(that.find(".foodMoney").text()*text);
		}

		if($("."+thisClass+"").find(".subtract-other").css("display") == "none"){
			$(".foodList ."+thisClass+"").remove();

			if($(".foodList li").html() == undefined){
				$(".foodList").css("display","none");
				$(".zhezhao").css("display","none");
			}
		}



		totalPrice = $("#totalPrice").text()-parseFloat($("."+thisClass+"").find(".foodMoney").eq(0).text());
		$("#totalPrice").text(totalPrice);
		
		//判断食物总个数
		if($(".settlement-car-num").text()==1){
			$(".settlement").css("display","none");
			$(".settlement-car-num").text(0);
			$("#settlementDiv").remove();
		}else{
			$(".settlement-car-num").text(parseFloat($(".settlement-car-num").text())-1);
		}
	}


	//点击结算跳转
	function toSubmit(e){
		e.stopPropagation();
		var Request = new Object();
		Request = GetRequest();
		var id = parseFloat(Request['id']);
		var restaurantName = $(".detail-top-name").text();

		var totalPrice = $("#totalPrice").text();
		var ids = new Array();
		var foodNum = new Array();
		var foodList = new Array();

		for(var i=0;i<$(".foodList .subtract-other").length;i++){
			if($(".foodList .subtract-other").eq(i).css("display")=="block"){
				
				ids.push($(".foodList .subtract-other").eq(i).parents("li").eq(0).attr("data-foodid"));
				foodNum.push(parseFloat($(".foodList .subtract-other").eq(i).find(".foodNum").text()));

				//新接口逻辑
				foodList.push({buyNum:parseFloat($(".foodList .subtract-other").eq(i).find(".foodNum").text()),id:$(".foodList .subtract-other").eq(i).parents("li").eq(0).attr("data-foodid"),price:$(".foodList .subtract-other").eq(i).parents("li").eq(0).find(".foodList_price_p").text()});
			}
		}
		var detailInfo = {"restaurantId":id,"ids":ids,"foodNum":foodNum,"totalPrice":totalPrice};
		sessionStorage.setItem("detailInfo",JSON.stringify(detailInfo));

		var restaurantPhone = $(".detail-top").attr("data-phone");//新接口逻辑
		
		checkOrderInfo(foodList,id,restaurantName,restaurantPhone);
		// window.location.href = "./submitOrder.html?restaurantId="+id;
		// window.location.href = "./submitOrder.html?ids="+ids+"&foodNum="+foodNum+"&totalPrice="+totalPrice+"&restaurantId="+id;
	}

	//点击跳出已选择的食物
	function disFood(){
		if($(".foodList").css("display")=="none"){
			$(".foodList").css("display","block");
			$(".zhezhao").css("display","block");
		}else{
			$(".foodList").css("display","none");
			$(".zhezhao").css("display","none");
		}
	}

	// 菜单滚动
	function menuScroll(){
		var scrollTop = 0;
		var offsetHeight = 0;
		var clientHeight = 0;
		var bottomHeight = 0;
		if($(".detail-food").eq(1).offset().top==undefined||$(".detail-food").eq(1).offset().top==null){
			var food = 0;
		}else{
			var food = $(".detail-food").eq(1).offset().top-796.4375;
		}
		
		var height = $(".detail-food").eq(0).height();
		// $("#menu").scrollTop(height);
		$("#menu").bind('scroll',function(){
			for(var i=0;i<$(".detail-food").length;i++){
				if($(".detail-food").eq(i).offset().top==undefined||$(".detail-food").eq(i).offset().top==null){
					var foodTop=0;
				}else{
					var foodTop = parseFloat($(".detail-food").eq(i).offset().top);
				}

				if((foodTop-796)>-100 && (foodTop-796)<100){
					scrollTop = $("#menu").scrollTop();//滚动条距离页面顶部距离
					offsetHeight = document.getElementById("menu").scrollHeight;//当前页面高度(包括滚动页面)
					clientHeight = $("#menu").height();//当前页面高度(不包括滚动页面)
					bottomHeight = scrollTop-(offsetHeight-clientHeight);
					// console.log("bottomHeight"+bottomHeight);
					if(bottomHeight>-10){
						$(".dish-menu li").eq(i).addClass("menu-current").siblings().removeClass("menu-current");
					}
				}
			}
				
				
				
				// console.log("bottomHeight"+bottomHeight);
		});
	}

	//菜单点击
	function menuClick(that){
		$("#menu").scrollTop(0);
		var thatP = $(that);
		thatP.addClass("menu-current").siblings().removeClass("menu-current");
		var index = thatP.index();

		var currentTop = $(".detail-food").eq(index).offset().top;
		if(currentTop==undefined||currentTop==null){
			var top = 796.4375;
		}else{
			var top = currentTop;
		}
		top = top-796.4375;
		$("#menu").scrollTop(top);
	}
	
	//调整菜单高度
	function menuHeight(){
		var leftHeight = $(".dish-menu").height();
		console.log(leftHeight);
		console.log($("#menu").height());
		if(leftHeight>$("#menu").height()){
			$("#menu").height(leftHeight+"px");
		}
	}

	//菜品分类列表对应菜品
	function findFoodCategoryList(){
		var Request = new Object();
		Request = GetRequest();
		var id = Request['id'];
	
		var obj = JSON.stringify({
			"restaurantId": id,
		});
	
		httpHelperpost(_API_FOOD_CATEGORY_LIST, obj, function(isSuccess, data) {
	        if (isSuccess == true) {
	            var response = data.responseText;
	            var value = JSON.parse(response);
	    			
	   			// console.log(response);
	   			var cateHtml = "";
	   			for(var i=0;i<value.data.length;i++){
	   				if(value.data[i].cateGoryId==1){
	   					cateHtml+="<li onclick='menuClick(this);' data-cateGoryId="+value.data[i].cateGoryId+" class='menu-current'><img src='./img/hot.png'>"+value.data[i].cateGoryName+"</li>"
	   				}else{
	   					if(i==0){
	   						cateHtml+="<li class='menu-current' onclick='menuClick(this);' data-cateGoryId="+value.data[i].cateGoryId+">"+value.data[i].cateGoryName+"</li>"
	   					}else{
	   						cateHtml+="<li onclick='menuClick(this);' data-cateGoryId="+value.data[i].cateGoryId+">"+value.data[i].cateGoryName+"</li>"
	   					}
	   					
	   				}
	   				
	   			}

	   			var menuHtml = "";
	   			for(var j=0;j<value.data.length;j++){
	   				if(value.data[j].foodList==""){
	   					menuHtml += "<ul class='detail-food'></ul>";
	   				}else{
	   					menuHtml += "<ul class='detail-food'>";
	   					for(var k=0;k<value.data[j].foodList.length;k++){
	   						menuHtml +="<li class='food"+value.data[j].foodList[k].id+"' data-foodid="+value.data[j].foodList[k].id+">" 
	   								+"<div class='detail-food-img'><img src="+value.data[j].foodList[k].picture+"></div>"
	   								+"<div class='detail-food-bar'>"
	   								+"<div class='detail-food-name'>"+value.data[j].foodList[k].name+"</div>"
	   								+"<div class='detail-food-num'><span>月售"+numisNull(value.data[j].foodList[k].salesVolume)+"</span></div>"
	   								+"<div class='detail-food-money'>￥<span class='foodMoney'>"+value.data[j].foodList[k].price+"</span></div>"
	   								+"</div>"
	   								+"<div class='detail-food-add'>"
	   								+"<span class='subtract-other'><img class='subtractNum' onclick='subtractFoodNum(this);' src='./img/subtract_01.png'><span class='foodNum'>1</span></span>"
	   								+"<img class='addNum' onclick='addFoodNum(this);' src='./img/add_01.png'>"
	   								+"</div>"
	   								+"</li>"
	   					}
	   					menuHtml += "</ul>";
	   				}
	   			}

	   			$(".dish-menu").html(cateHtml);
	   			$("#menu").append(menuHtml);

	
	            if(value.code == 0){
	             	console.log("查询成功");
	             	if($(".detail-food li").length>4){
   						menuScroll();//菜单滚动
   					}
	             	// menuScroll(); 

	             	var detailInfo = sessionStorage.getItem("detailInfo");
	             	
	             	if(detailInfo == undefined||detailInfo==""||detailInfo==null){

	             	}
	             	else{
	             		detailInfo = JSON.parse(detailInfo);
	             		var ids = detailInfo.ids;
	             		// var foodNum = Request['foodNum'];
	             		// var totalPrice = Request['totalPrice'];
	             		// $(".settlement").css("display","block");
	             		var foodNum = detailInfo.foodNum;
	             		var totalPrice = detailInfo.totalPrice;
	             		var totalCount = 0;
	             		var foodHtml = "";
	             		var thisClass = "";
	             		
	             		for(var i=0;i<ids.length;i++){
	             			
	             			// for(var j=0;j<$(".detail-food li").length;j++){

	          //    				if(ids[i] == $(".detail-food li").eq(j).attr("data-foodid")){
	          //    					$(".detail-food li").eq(j).find(".foodNum").text(foodNum[i]);
	          //    					$(".detail-food li").eq(j).find(".subtract-other").css("display","block");
	          //    					thisClass = $(".detail-food li").eq(j).attr("class");
	          //    					foodHtml += "<li class="+thisClass+"><div class='foodList_name'>"+$("."+thisClass+"").find(".detail-food-name").text()+"</div>"
											// +"<div class='foodList_price'>￥<span>"+$("."+thisClass+"").find(".foodMoney").text()+"</span></div>"
											// +"<div class='sub-add'>"
											// +"<span class='subtract-other'><img class='subtractNum' src='./img/subtract_01.png' onclick='subtractFoodNum(this);'><span class='foodNum'>"+$("."+thisClass+"").find(".foodNum").text()+"</span></span>"
											// +"<img class='addNum' src='./img/add_01.png' onclick='addFoodNum(this);'>"
											// +"</div></li>";
									
	          //    				}
	             			// }
	             			thisClass = "food"+ids[i];
	             			foodHtml += "<li class="+thisClass+" data-foodid="+ids[i]+"><div class='foodList_name'>"+$("."+thisClass+"").find(".detail-food-name").eq(0).text()+"</div>"
											+"<div class='foodList_price'>￥<span class='foodList_price_p'>"+$("."+thisClass+"").find(".foodMoney").eq(0).text()+"</span></div>"
											+"<div class='sub-add'>"
											+"<span class='subtract-other'><img class='subtractNum' src='./img/subtract_01.png' onclick='subtractFoodNum(this);'><span class='foodNum'>"+foodNum[i]+"</span></span>"
											+"<img class='addNum' src='./img/add_01.png' onclick='addFoodNum(this);'>"
											+"</div></li>";
	             			totalCount += parseFloat(foodNum[i]);
	             			$("."+thisClass+"").find(".foodNum").text(foodNum[i]);
	             			$("."+thisClass+"").find(".subtract-other").css("display","block");
	             		}

	             		$(".settlement").css("display","block");
	             		$("#totalPrice").text(totalPrice);
	             		$(".settlement-car-num").text(totalCount);
	             		$(".foodList ul").append(foodHtml);
	             	}
	             	
	             	
	             	menuHeight();//调整菜单高度
	             	
	             	
	            }else if(value.code==401){
	            	alert(value.message);
	            	window.location.href='./index.html';
	            }else{
	            	alert(value.message);
	             	console.log("查询失败");
	            }
	        }else {}
	    });
	}

	
	
	//餐厅信息
	function getRestaurantInfo(){
		var Request = new Object();
		Request = GetRequest();
		var id = Request['id'];
		
		var detailInfo = sessionStorage.getItem("detailInfo");
		if(detailInfo==null||detailInfo==undefined||detailInfo==""){
			
		}else{
			detailInfo = JSON.parse(detailInfo);
			var currentId = detailInfo.restaurantId;
			if(id==currentId){
				
			}else{
				sessionStorage.setItem("detailInfo","");
			}
		}
		// var detailInfo = JSON.parse(sessionStorage.getItem("detailInfo"));
		// var currentId = detailInfo.restaurantId;
		

		var obj = JSON.stringify({
			"id": id,
		});
		
		httpHelperpost(_API_GETRESTAURANT_INFO, obj, function(isSuccess, data) {
	        if (isSuccess == true) {
	            var response = data.responseText;
	            var value = JSON.parse(response);
	    			
	   			// console.log(response);
	   			var dispatchPrice = value.data.dispatchPrice;
	   			$(".logoImg").attr("src",value.data.icon);
	   			$(".detail-top-name").text(value.data.name);
	   			$("#dispatchPrice").text(dispatchPrice==null?dispatchPrice=0:dispatchPrice);
	   			// $("#totalPrice").text(dispatchPrice);
	   			var activity = new Array();
	   			activity = value.data.fullReduceActivity.split(";");
	   			var activityHtml = "";
	   			for(var i=0;i<activity.length-1;i++){
	   				activityHtml += "<span>"+activity[i]+"</span>";
	   			}
	   			$(".prefer-inc").html(activityHtml);
	            if(value.code == 0){
	             	console.log("查询成功");
	             	$(".detail-top").attr("data-phone",value.data.phone);
	            }else if(value.code==401){
	            	alert(value.message);
	            	window.location.href='./index.html';
	            }else{
	            	alert(value.message);
	             	console.log("查询失败");
	            }
	        }else {}
	    });
	}

	//清空已选中的食物列表
	function clearFoodList(){
		$("#menu").find(".foodNum").text(1);
		$("#menu").find(".subtract-other").css("display","none");
		$(".settlement").css("display","none");
		$(".settlement-car-num").text(0);
		$("#totalPrice").text(0);
		$(".foodList ul li").remove();
		disFood();
		sessionStorage.setItem("detailInfo","");
	}

	//数字判空函数
	function numisNull(value){
		if(value==null||value==undefined){
			value = 0;
		}
		return value;
	}

	//获取url中"?"符后的字串
	function GetRequest() {
	    var url = location.search; //获取url中"?"符后的字串   
	    
	    var theRequest = new Object();
	    if (url.indexOf("?") != -1) {
	      var str = url.substr(1);
	      
	      strs = str.split("&");
	      
	      for (var i = 0; i < strs.length; i++) {
	        
	        theRequest[strs[i].split("=")[0]] = strs[i]
	            .split("=")[1];
	      }
	    }
	    return theRequest;
	}

	function detailDis(url){
		var Request = new Object();
        Request = GetRequest();
        var id = Request['id'];
        var totalPrice = $("#totalPrice").text();
		var ids = new Array();
		var foodNum = new Array();

		for(var i=0;i<$(".detail-food .subtract-other").length;i++){
			if($(".subtract-other").eq(i).css("display")=="block"){
				ids.push(parseFloat($(".subtract-other").eq(i).parents("li").eq(0).attr("data-foodid")));
				foodNum.push(parseFloat($(".subtract-other").eq(i).find(".foodNum").text()));
			}
		}
		window.location.href = './'+url+".html?id="+id;
		// var detailInfo = {"restaurantId":id,"ids":ids,"foodNum":foodNum,"totalPrice":totalPrice};
		// sessionStorage.setItem("detailInfo",JSON.stringify(detailInfo));

        // window.location.href = './'+url+".html?id="+id+"&ids="+ids+"&foodNum="+foodNum+"&totalPrice="+totalPrice;
	}

	//订单-确认订单信息
	function checkOrderInfo(foodList,restaurantId,restaurantName,restaurantPhone){
        var Request = new Object();
        Request = GetRequest();
        var id = Request['id'];
		var dispatchId = 1;
		var userinfo = sessionStorage.getItem("userInfo");
		userinfo = JSON.parse(userinfo);
		console.log(userinfo);
		var userPhone = userinfo.username;
		var obj = JSON.stringify({
			"dispatchId": dispatchId,
			"foodList":foodList,
			"restaurantId":id,
			"restaurantName":restaurantName,
			"restaurantPhone":restaurantPhone,
			"userPhone":userPhone
		});
		httpHelperpost("/api/foodOrders/checkOrderInfo", obj, function(isSuccess, data) {
	        if (isSuccess == true) {
	            var response = data.responseText;
	            var value = JSON.parse(response);
	    			
	   			console.log(response);
	   			
	            if(value.code == 0){
	             	console.log("查询成功");

	             	sessionStorage.setItem("checkOrderInfo",JSON.stringify(value.data));
	             	sessionStorage.setItem("checkOrderObj",JSON.stringify({foodList:foodList,restaurantId:id,restaurantName:restaurantName,restaurantPhone:restaurantPhone,userPhone:userPhone}));
	             	
	             	
	             	window.location.href = "./submitOrder.html?restaurantId="+id;
	            }else if(value.code==401){
	            	alert(value.message);
	            	window.location.href='./index.html';
	            }else{
	            	alert(value.message);
	             	console.log("查询失败");
	            }
	        }else {}
	    });
	}